package com.DAO;

import com.JdbcUtils;
import com.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    // 获取数据库连接
    private Connection getConnection() throws SQLException {
        return JdbcUtils.getConnection(); // 使用你提供的 JdbcUtils 获取连接
    }

    // 通过用户名查找用户
    public User getUserByUsername(String username) throws SQLException {
        String sql = "SELECT * FROM user WHERE username = ?";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                //user.setCreatedAt(rs.getDate("created_at"));
                return user;
            }
        }
        return null;
    }

    // 检查用户名是否已存在
    public boolean isUsernameExist(String username) throws SQLException {
        String sql = "SELECT COUNT(*) FROM user WHERE username = ?";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1) > 0; // 如果返回的计数大于 0，表示用户名已存在
            }
        }
        return false;
    }

    // 保存新用户
    public void saveUser(User user) throws SQLException {
        String sql = "INSERT INTO user (username, password, email) VALUES (?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            ps.executeUpdate();
        }
    }

    // 删除用户
    public void deleteUser(int id) {
        String sql = "DELETE FROM user WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    // 通过id获取用户
    public User getUserById(int id) throws SQLException{
        User user = null;
        String sql = "SELECT * FROM user WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
            }
             }
        return user;
    }

    // 获取所有用户
    public List<User> getAllUsers() throws SQLException {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM user";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                users.add(user);
            }
            System.out.println("从数据库获取的用户数量: " + users.size());  // 调试信息
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;  // 抛出异常
        }
        return users;
    }


    // 更新用户
    public void updateUser(User user) throws SQLException{
        String sql = "UPDATE user SET username = ?, password = ?, email = ? WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            ps.setInt(4, user.getId());
            ps.executeUpdate();
        }
    }

    // 添加新用户
    public void addUser(User user) throws SQLException{
        // 执行sql语句，向数据库插入数据
        System.out.print("添加用户");
        String sql = "INSERT INTO user ( username, password, email) VALUES (?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            //ps.setInt(4, user.getId());
            ps.executeUpdate();

        }

    }
}
